﻿using CourseManagement.Service;
using CourseManagerment.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CourseManagerment.ViewModel
{
    public class LoginViewModel : NotifyBase
    {
        /// <summary>
        /// 属性类
        /// </summary>
        public LoginModel LoginModel { get; set; }
        /// <summary>
        /// 交互命令基类：关闭按钮的关闭窗体方法
        /// </summary>
        public CommandBase CloseWindowCommand { get; set; } = new() { DoCanExecute = e => true };
        /// <summary>
        /// 登陆按钮的登陆方法
        /// </summary>
        public CommandBase LoginCommand { get; set; } = new() { DoCanExecute = e => true };
        private string _errorMessage;
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; this.DoNotify(); }
        }
        public LoginViewModel()
        {
            LoginModel = new();
#if DEBUG
            LoginModel.UserName = "admin";
            LoginModel.Password = "123456";
            LoginModel.ImagePath = @"/Assets/Images/验证码.png";
#endif
            CloseWindowCommand.DoExecute = o => { (o as Window).Close(); };
            LoginCommand.DoExecute = DoLogin;
        }
        private void DoLogin(object o)
        {
#if DEBUG
            (o as Window).DialogResult = true;
#else
   LoginModel.ShowProgress = Visibility.Visible;
            this.ErrorMessage = "";
            if (string.IsNullOrEmpty(LoginModel.UserName))
            {
                this.ErrorMessage = "请输入用户名！";
                LoginModel.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.Password))
            {
                this.ErrorMessage = "请输入密码！";
                LoginModel.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.ValidationCode))
            {
                this.ErrorMessage = "请输入验证码！";
                LoginModel.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (LoginModel.ValidationCode.ToLower()!="123")
            {
                this.ErrorMessage = "验证码输入不正确！";
                LoginModel.ShowProgress = Visibility.Collapsed;
                return;
            }
            //新建线程，防止卡主
            Task.Run(new Action(() =>
            {
                try
                {
                    var user = LocalDataAccess.GetInstance().CheckUserInfo(LoginModel.UserName, LoginModel.Password);
                    GlobalValues.UserInfo = user ?? throw new Exception("登录失败！用户名或密码错误！");//数据库取出的用户信息保存全局变量
                   
                    //运行主窗体
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        ((Window)o).DialogResult = true;
                    }));

                }
                catch (Exception e)
                {
                    this.ErrorMessage = e.Message;
                }
                finally
                {
                    Application.Current.Dispatcher.Invoke(new Action(() => { LoginModel.ShowProgress = Visibility.Collapsed; }));
                }
            }));
#endif

        }
    }
}
